package com.umeng.soexample;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.umeng.socialize.shareboard.ShareBoardConfig;
import com.umeng.socialize.shareboard.SnsPlatform;
import com.umeng.socialize.utils.Log;
import com.umeng.socialize.utils.ShareBoardlistener;
import com.umeng.soexample.model.Defaultcontent;

import java.lang.ref.WeakReference;


/**
 * Created by wangfei on 16/11/10.
 */
public class ShareMenuActivity extends Activity {

	private UMShareListener mShareListener;
	private ShareAction mShareAction;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
			Window window = getWindow();
			window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
			window.setStatusBarColor(getResources().getColor(R.color.umeng_blue));
		}

		setContentView(R.layout.umeng_menu);

		((TextView) findViewById(R.id.umeng_title)).setText(R.string.umeng_menu_title);

		findViewById(R.id.umeng_back).setVisibility(View.VISIBLE);

		findViewById(R.id.umeng_back).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				finish();
			}
		});

		mShareListener = new CustomShareListener(this);

        /*增加自定义按钮的分享面板*/
		mShareAction = new ShareAction(ShareMenuActivity.this).setDisplayList(//

				SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.WEIXIN_FAVORITE,//
				SHARE_MEDIA.SINA, SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE,//
				SHARE_MEDIA.ALIPAY, SHARE_MEDIA.RENREN, SHARE_MEDIA.DOUBAN,//
				SHARE_MEDIA.SMS, SHARE_MEDIA.EMAIL, SHARE_MEDIA.YNOTE,//
				SHARE_MEDIA.EVERNOTE, SHARE_MEDIA.LAIWANG, SHARE_MEDIA.LAIWANG_DYNAMIC,//
				SHARE_MEDIA.LINKEDIN, SHARE_MEDIA.YIXIN, SHARE_MEDIA.YIXIN_CIRCLE,//
				SHARE_MEDIA.TENCENT, SHARE_MEDIA.FACEBOOK, SHARE_MEDIA.TWITTER,//
				SHARE_MEDIA.WHATSAPP, SHARE_MEDIA.GOOGLEPLUS, SHARE_MEDIA.LINE,//
				SHARE_MEDIA.INSTAGRAM, SHARE_MEDIA.KAKAO, SHARE_MEDIA.PINTEREST,//
				SHARE_MEDIA.POCKET, SHARE_MEDIA.TUMBLR, SHARE_MEDIA.FLICKR,//
				SHARE_MEDIA.FOURSQUARE, SHARE_MEDIA.MORE)//

				.addButton("umeng_sharebutton_copy",//
						"umeng_sharebutton_copy",//
						"umeng_socialize_copy",//
						"umeng_socialize_copy")//

				.addButton("umeng_sharebutton_copyurl",//
						"umeng_sharebutton_copyurl", //
						"umeng_socialize_copyurl",//
						"umeng_socialize_copyurl")//

				.setShareboardclickCallback(new ShareBoardlistener() {//
					@Override
					public void onclick(SnsPlatform snsPlatform, SHARE_MEDIA share_media) {//
						if (snsPlatform.mShowWord.equals("umeng_sharebutton_copy")) {//
							Toast.makeText(ShareMenuActivity.this, "复制文本按钮", Toast.LENGTH_LONG).show();//
						} else if (snsPlatform.mShowWord.equals("umeng_sharebutton_copyurl")) {//
							Toast.makeText(ShareMenuActivity.this, "复制链接按钮", Toast.LENGTH_LONG).show();//

						} else {//
							UMWeb web = new UMWeb(Defaultcontent.url);//
							web.setTitle("来自分享面板标题");//
							web.setDescription("来自分享面板内容");//
							web.setThumb(new UMImage(ShareMenuActivity.this, R.drawable.logo));//
							new ShareAction(ShareMenuActivity.this).withMedia(web)//
									.setPlatform(share_media)//
									.setCallback(mShareListener)//
									.share();//
						}
					}
				});
        /*无自定按钮的分享面板*/
		//        mShareAction = new ShareAction(ShareMenuActivity.this).setDisplayList(
		//                SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.WEIXIN_FAVORITE,
		//                SHARE_MEDIA.SINA, SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE,
		//                SHARE_MEDIA.ALIPAY, SHARE_MEDIA.RENREN, SHARE_MEDIA.DOUBAN,
		//                SHARE_MEDIA.SMS, SHARE_MEDIA.EMAIL, SHARE_MEDIA.YNOTE,
		//                SHARE_MEDIA.EVERNOTE, SHARE_MEDIA.LAIWANG, SHARE_MEDIA.LAIWANG_DYNAMIC,
		//                SHARE_MEDIA.LINKEDIN, SHARE_MEDIA.YIXIN, SHARE_MEDIA.YIXIN_CIRCLE,
		//                SHARE_MEDIA.TENCENT, SHARE_MEDIA.FACEBOOK, SHARE_MEDIA.TWITTER,
		//                SHARE_MEDIA.WHATSAPP, SHARE_MEDIA.GOOGLEPLUS, SHARE_MEDIA.LINE,
		//                SHARE_MEDIA.INSTAGRAM, SHARE_MEDIA.KAKAO, SHARE_MEDIA.PINTEREST,
		//                SHARE_MEDIA.POCKET, SHARE_MEDIA.TUMBLR, SHARE_MEDIA.FLICKR,
		//                SHARE_MEDIA.FOURSQUARE, SHARE_MEDIA.MORE)
		//                .withText(Defaultcontent.text + "来自友盟自定义分享面板")
		//                .setCallback(mShareListener);

		findViewById(R.id.umeng_menu_bottom).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mShareAction.open();
			}
		});
		findViewById(R.id.umeng_menu_center).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				ShareBoardConfig config = new ShareBoardConfig();//
				config.setShareboardPostion(ShareBoardConfig.SHAREBOARD_POSITION_CENTER);//
				config.setMenuItemBackgroundShape(ShareBoardConfig.BG_SHAPE_CIRCULAR); // 圆角背景

				//                config.setTitleVisibility(false); // 隐藏title
				//                config.setCancelButtonVisibility(false); // 隐藏取消按钮
				mShareAction.open(config);//

			}
		});

		findViewById(R.id.umeng_menu_bottom2).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				ShareBoardConfig config = new ShareBoardConfig();//
				config.setMenuItemBackgroundShape(ShareBoardConfig.BG_SHAPE_NONE);//
				mShareAction.open(config);//

			}
		});

		findViewById(R.id.umeng_menu_center2).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				ShareBoardConfig config = new ShareBoardConfig();//
				config.setShareboardPostion(ShareBoardConfig.SHAREBOARD_POSITION_CENTER);
				config.setMenuItemBackgroundShape(ShareBoardConfig.BG_SHAPE_NONE);
				mShareAction.open(config);
			}
		});
	}

	private static class CustomShareListener implements UMShareListener {

		private WeakReference<ShareMenuActivity> mActivity;

		private CustomShareListener(ShareMenuActivity activity) {
			mActivity = new WeakReference(activity);
		}

		@Override
		public void onStart(SHARE_MEDIA platform) {

		}

		@Override
		public void onResult(SHARE_MEDIA platform) {

			if (platform.name().equals("WEIXIN_FAVORITE")) {//
				Toast.makeText(mActivity.get(), platform + " 收藏成功啦", Toast.LENGTH_SHORT).show();//
			} else {
				if (platform != SHARE_MEDIA.MORE && platform != SHARE_MEDIA.SMS//
						&& platform != SHARE_MEDIA.EMAIL//
						&& platform != SHARE_MEDIA.FLICKR//
						&& platform != SHARE_MEDIA.FOURSQUARE//
						&& platform != SHARE_MEDIA.TUMBLR//
						&& platform != SHARE_MEDIA.POCKET//
						&& platform != SHARE_MEDIA.PINTEREST//

						&& platform != SHARE_MEDIA.INSTAGRAM//
						&& platform != SHARE_MEDIA.GOOGLEPLUS//
						&& platform != SHARE_MEDIA.YNOTE//
						&& platform != SHARE_MEDIA.EVERNOTE) {//
					Toast.makeText(mActivity.get(), platform + " 分享成功啦", Toast.LENGTH_SHORT).show();
				}

			}
		}

		@Override
		public void onError(SHARE_MEDIA platform, Throwable t) {//
			if (platform != SHARE_MEDIA.MORE && platform != SHARE_MEDIA.SMS//
					&& platform != SHARE_MEDIA.EMAIL//
					&& platform != SHARE_MEDIA.FLICKR//
					&& platform != SHARE_MEDIA.FOURSQUARE//
					&& platform != SHARE_MEDIA.TUMBLR//
					&& platform != SHARE_MEDIA.POCKET//
					&& platform != SHARE_MEDIA.PINTEREST//

					&& platform != SHARE_MEDIA.INSTAGRAM//
					&& platform != SHARE_MEDIA.GOOGLEPLUS//
					&& platform != SHARE_MEDIA.YNOTE//
					&& platform != SHARE_MEDIA.EVERNOTE) {//
				Toast.makeText(mActivity.get(), platform + " 分享失败啦", Toast.LENGTH_SHORT).show();//
				if (t != null) {//
					Log.d("throw", "throw:" + t.getMessage());//
				}//
			}

		}

		@Override
		public void onCancel(SHARE_MEDIA platform) {

			Toast.makeText(mActivity.get(), platform + " 分享取消了", Toast.LENGTH_SHORT).show();//
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		/** attention to this below ,must add this**/
		UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
	}

	/**
	 * 屏幕横竖屏切换时避免出现window leak的问题
	 */
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		mShareAction.close();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		//   UMShareAPI.get(this).release();
	}
}
